`
https://leetcode.cn/problems/smallest-string-starting-from-leaf/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {string}
 */
var smallestFromLeaf = function (root) {
  const res = []
  const path = []

  // 构建映射表：0 -> a, ..., 25 -> z
  const map = {}
  for (let i = 0; i <= 25; i++) {
    map[i] = String.fromCharCode('a'.charCodeAt(0) + i)
  }

  const traverse = (root) => {
    if (root === null) {
      return
    }

    // 将当前值对应的字符放入路径
    path.push(map[root.val])

    // 如果到达叶子结点，记录字符串
    if (root.left === null && root.right === null) {
      res.push(path.toReversed().join(''))
    }

    // 递归遍历
    traverse(root.left)
    traverse(root.right)

    // 从路径中取出当前值
    path.pop()
  }

  traverse(root)

  res.sort()

  return res[0]
};